package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.web.controller.demo.domain.CarList;
import com.ruoyi.web.controller.demo.domain.CarUseRecord;
import com.ruoyi.web.controller.service.ICarListService;
import com.ruoyi.web.controller.service.ICarUseRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.demo.domain.CarProcess;
import com.ruoyi.web.controller.service.ICarProcessService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆申请Controller
 * 
 * @author ruoyi
 * @date 2022-12-26
 */
@Controller
@RequestMapping("/system/process")
public class CarProcessController extends BaseController
{
    private String prefix = "system/process";

    @Autowired
    private ICarProcessService carProcessService;
    @Autowired
    private ICarListService carListService;
    @Autowired
    private ICarUseRecordService carUseRecordService;


    @RequiresPermissions("system:process:view")
    @GetMapping()
    public String process()
    {
        return prefix + "/process";
    }

    /**
     * 查询车辆申请列表
     */
    @RequiresPermissions("system:process:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarProcess carProcess)
    {
        startPage();
        List<CarProcess> list = carProcessService.selectCarProcessList(carProcess);
        return getDataTable(list);
    }

    /**
     * 导出车辆申请列表
     */
    @RequiresPermissions("system:process:export")
    @Log(title = "车辆申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarProcess carProcess)
    {
        List<CarProcess> list = carProcessService.selectCarProcessList(carProcess);
        ExcelUtil<CarProcess> util = new ExcelUtil<CarProcess>(CarProcess.class);
        return util.exportExcel(list, "车辆申请数据");
    }

    /**
     * 新增车辆申请
     */
    @GetMapping("/add/{carId}")
    public String add(@PathVariable("carId") Long carId,ModelMap mmap)
    {

        CarList carList = carListService.selectCarListByCarId(carId);
        mmap.put("car",carList);
        SysUser sysUser = getSysUser();
        mmap.put("sysUser", sysUser);
        return prefix + "/add";
    }

    /**
     * 新增保存车辆申请
     */
    @RequiresPermissions("system:process:add")
    @Log(title = "车辆申请", businessType = BusinessType.INSERT)
    @PostMapping("/add/{carId}")
    @ResponseBody
    public AjaxResult addSave(@PathVariable("carId") Long carId, CarProcess carProcess, CarList carList, CarUseRecord carUseRecord)
    {
        carList.setCarId(carId);
        int i = carListService.updateCarStatusNormal(carId);
        Date date = new Date();
        carProcess.setCreatedTime(date);
        carProcessService.insertCarProcess(carProcess);
        carUseRecord.setRecordCar(carProcess.getUsedCar());
        carUseRecord.setOperationName("申请");
        carUseRecord.setCreater(getSysUser().getUserId());
        carUseRecord.setCreatedTime(new Date());
        return toAjax(carUseRecordService.insertCarUseRecord(carUseRecord));
    }

    /**
     * 修改车辆申请
     */
    @RequiresPermissions("system:process:edit")
    @GetMapping("/edit/{processId}")
    public String edit(@PathVariable("processId") Long processId, ModelMap mmap)
    {
        CarProcess carProcess = carProcessService.selectCarProcessByProcessId(processId);
        mmap.put("carProcess", carProcess);
        return prefix + "/edit";
    }


    @GetMapping("/back/{usedCar}")
    @ResponseBody
    public AjaxResult getBack(@PathVariable("usedCar") Long usedCar, CarUseRecord carUseRecord){
        carProcessService.updateCarStatus(usedCar);
        carUseRecord.setRecordCar(usedCar);
        carUseRecord.setOperationName("归还");
        carUseRecord.setCreater(getSysUser().getUserId());
        carUseRecord.setCreatedTime(new Date());
        carUseRecordService.insertCarUseRecord(carUseRecord);
        return toAjax(carListService.updateCarStatusSend(usedCar));
    }
    /**
     * 修改保存车辆申请
     */
    @RequiresPermissions("system:process:edit")
    @Log(title = "车辆申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarProcess carProcess)
    {
        return toAjax(carProcessService.updateCarProcess(carProcess));
    }

    /**
     * 删除车辆申请
     */
    @RequiresPermissions("system:process:remove")
    @Log(title = "车辆申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carProcessService.deleteCarProcessByProcessIds(ids));
    }


}
